Dynomotion

Group: DynoMotion Message: 6823 From: himykabibble Date: 2/27/2013
Subject: Can One Thread Create Another Thread?
Tom,

Is there any mechanism whereby a program in one thread can launch a different program in another thread? I have an application where this would be helpful.

Regards,
Ray L.
Group: DynoMotion Message: 6824 From: Lee Studley Date: 2/27/2013
Subject: Re: Can One Thread Create Another Thread?
Hi Ray, ( just my 2pennies)
Not sure what tom will say, but I think you could use the"Persist Array"
and create a location for
global user thread flags.

Then a thread could check the flag state to either execute or do nothing.
There are "Get" and "Set" functions to call for this.
-Lee Studley

>>>> from the Docs
GetPersistDec<N>
Description
Read a single word from the Persist Array at the specified offset a
single 32-bit value displayed as a signed decimal number. The persist
array is a general purpose array of N_USER_DATA_VARS (2
C Programs may access this array as the integer array:

persist.UserData[n];
It also resides in the KMotion Persist memory structure so that if
memory is flashed, the value will be present at power up.
See also GetPersistHex, SetPersistDec, SetPersistHex
Parameters
<N>
Offset into the integer array. Valid range 0...199.
Example: GetPersistDec 10



GetPersistHex<N>
Description
Read a single word from the Persist Array at the specified offset a
single 32-bit value displayed as an unsigned hexadecimal number. The
persist array is a general purpose array of N_USER_DATA_VARS (200)
32-bit values that is accessible to the host as well as KMotion C
Programs. It may be used to share parameters, commands, or information
between programs.
C Programs may access this array as the integer array:
persist.UserData[n];

It also resides in the KMotion Persist memory structure so that if
memory is flashed, the value will be present at power up.
See also GetPersistDec, SetPersistDec, SetPersistHex
Parameters
<N>

Offset into the integer array. Valid range 0...199.
Example: GetPersistHex 10


On 2/27/2013 9:47 AM, himykabibble wrote:
> Tom,
>
> Is there any mechanism whereby a program in one thread can launch a different program in another thread? I have an application where this would be helpful.
>
> Regards,
> Ray L.
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>
Group: DynoMotion Message: 6825 From: Tom Kerekes Date: 2/27/2013
Subject: Re: Can One Thread Create Another Thread?
Hi Ray,

There isn't really a method to download a program from another Thread but if it is already downloaded it can be Launched with StartThread() as described in KMotionDef.h

A workaround in KMotionCNC to download a program to a Thread is to assign a hidden User Button to a program and then invoke the User Button from KFLOP.

HTH
Regards
TK

// M U L T I - T H R E A D   S U P P O R T

// user threads are numbered 1 .. n

void StartThread(int thread);  // starts a downloaded program at it's entry point
void PauseThread(int thread);  // stops a thread from executing
int ResumeThread(int thread);  // resumes a thread after a pause 
void ThreadDone(void);         // call to terminate current thread


Group: DynoMotion Message: 6826 From: himykabibble Date: 2/27/2013
Subject: Re: Can One Thread Create Another Thread?
Tom,

OK, that will do.

BTW - Just out of curiosity.... Does the interpreter allow nesting of programs? i.e. - I have a DSP program that handles M6. What happens if that DSP programs tries to execute a line of G-code through PCComm? I prevent that from happening in my app, but I'm wondering if I really need to or not? I've found a better work-around for this particular problem, but I'm sure it will surface again at some point in a different context.

Regards,
Ray L.

--- In DynoMotion@yahoogroups.com, Tom Kerekes <tk@...> wrote:
>
> Hi Ray,
>
> There isn't really a method to download a program from another Thread but if it is already downloaded it can be Launched with StartThread() as described in KMotionDef.h
>
> A workaround in KMotionCNC to download a program to a Thread is to assign a hidden User Button to a program and then invoke the User Button from KFLOP.
>
> HTH
> Regards
> TK
>
> // M U L T I - T H R E A D   S U P P O R T
>
> // user threads are numbered 1 .. n
>
> void StartThread(int thread);  // starts a downloaded program at it's entry point
> void PauseThread(int thread);  // stops a thread from executing
> int ResumeThread(int thread);  // resumes a thread after a pause 
> void ThreadDone(void);         // call to terminate current thread
>
>
>
> ________________________________
> From: himykabibble <jagboy1964@...>
> To: DynoMotion@yahoogroups.com
> Sent: Wednesday, February 27, 2013 8:47 AM
> Subject: [DynoMotion] Can One Thread Create Another Thread?
>
>
>  
> Tom,
>
> Is there any mechanism whereby a program in one thread can launch a different program in another thread? I have an application where this would be helpful.
>
> Regards,
> Ray L.
>
Group: DynoMotion Message: 6827 From: himykabibble Date: 2/27/2013
Subject: Re: Can One Thread Create Another Thread?
Lee,

I want to avoid the overhead of having another thread running all the time, when it will very rarely be needed to do anything (only during toolchanges, and only when the ATC screws up).

Regards,
Ray L.

--- In DynoMotion@yahoogroups.com, Lee Studley <indigo_red@...> wrote:
>
> Hi Ray, ( just my 2pennies)
> Not sure what tom will say, but I think you could use the"Persist Array"
> and create a location for
> global user thread flags.
>
> Then a thread could check the flag state to either execute or do nothing.
> There are "Get" and "Set" functions to call for this.
> -Lee Studley
>
> >>>> from the Docs
> GetPersistDec<N>
> Description
> Read a single word from the Persist Array at the specified offset a
> single 32-bit value displayed as a signed decimal number. The persist
> array is a general purpose array of N_USER_DATA_VARS (2
> C Programs may access this array as the integer array:
>
> persist.UserData[n];
> It also resides in the KMotion Persist memory structure so that if
> memory is flashed, the value will be present at power up.
> See also GetPersistHex, SetPersistDec, SetPersistHex
> Parameters
> <N>
> Offset into the integer array. Valid range 0...199.
> Example: GetPersistDec 10
>
>
>
> GetPersistHex<N>
> Description
> Read a single word from the Persist Array at the specified offset a
> single 32-bit value displayed as an unsigned hexadecimal number. The
> persist array is a general purpose array of N_USER_DATA_VARS (200)
> 32-bit values that is accessible to the host as well as KMotion C
> Programs. It may be used to share parameters, commands, or information
> between programs.
> C Programs may access this array as the integer array:
> persist.UserData[n];
>
> It also resides in the KMotion Persist memory structure so that if
> memory is flashed, the value will be present at power up.
> See also GetPersistDec, SetPersistDec, SetPersistHex
> Parameters
> <N>
>
> Offset into the integer array. Valid range 0...199.
> Example: GetPersistHex 10
>
>
> On 2/27/2013 9:47 AM, himykabibble wrote:
> > Tom,
> >
> > Is there any mechanism whereby a program in one thread can launch a different program in another thread? I have an application where this would be helpful.
> >
> > Regards,
> > Ray L.
> >
> >
> >
> > ------------------------------------
> >
> > Yahoo! Groups Links
> >
> >
> >
> >
>
Group: DynoMotion Message: 6828 From: Tom Kerekes Date: 2/27/2013
Subject: Re: Can One Thread Create Another Thread?
Hi Ray,

No the Interpreter is not re-entrant.  You should get an error if you attempt it.

Regards
TK

Group: DynoMotion Message: 6830 From: Neil Date: 2/27/2013
Subject: Re: Can One Thread Create Another Thread?
--- In DynoMotion@yahoogroups.com, "himykabibble" <jagboy1964@...> wrote:
>
> Tom,
>
> Is there any mechanism whereby a program in one thread can launch a different program in another thread? I have an application where this would be helpful.
>
> Regards,
> Ray L.
>

Just a thought Ray,

When different parts of a program change a common variable race conditions often occur.
One way to handle this situation is to check whether a semaphore has been set.
This really only works properly if the variable is decremented and tested all in one instruction, or can't be interrupted during the decrement and test cycle.
It works like this:
Task needs to lock semaphore.
It decrements the variable.
Is it less than zero? Bingo! It owns it. Increment it twice or add two if you can be sure nothing will interfere with the addition.
It is now one. YOU ARE THE OWNER
If it doesn't become negative, you don't own it. Increment it back to previous state.

If you become the owner, when done with the semaphore decrement it. (probably back to zero).
When you don't own it, wait a while then try to acquire it again.
A well behaved task returns the semaphore to zero in a reasonable amount of time.
The acquiring task on failure to acquire waits for some time, then tries again.
Optionally, after X failures it gives up and reports an error, probably by incrementing some other semaphore.

You can use semaphore like this to have another task automatically launch a task elsewhere - a timer can monitor semaphore and launch a new task.
The launched task decides whether the semaphore is available for further callers.

Making all these triggers occur once per whatever cycle, in most cases will prevent race conditions. They become wait conditions, with optional timeout and the caller can even have knowledge of whether the task is active.

I have found this to give reliable synchronization of asynchronous tasks.

Neil
Group: DynoMotion Message: 6832 From: himykabibble Date: 2/27/2013
Subject: Re: Can One Thread Create Another Thread?
Neil,

Synchronization is not an issue in this case. The problem is I needed some DSP code to run at a particular time, when the thread that normally runs that code was not available. I've found a simple work-around that has resolved the issue very nicely, and it's all working perfectly. Being able to do a fork/join would've been handy, but the solution I used is next-best, and just required duplicating a small amount of code.

Regards,
Ray L.

--- In DynoMotion@yahoogroups.com, "Neil" <neilw20@...> wrote:
>
>
>
>
>
>
> --- In DynoMotion@yahoogroups.com, "himykabibble" <jagboy1964@> wrote:
> >
> > Tom,
> >
> > Is there any mechanism whereby a program in one thread can launch a different program in another thread? I have an application where this would be helpful.
> >
> > Regards,
> > Ray L.
> >
>
> Just a thought Ray,
>
> When different parts of a program change a common variable race conditions often occur.
> One way to handle this situation is to check whether a semaphore has been set.
> This really only works properly if the variable is decremented and tested all in one instruction, or can't be interrupted during the decrement and test cycle.
> It works like this:
> Task needs to lock semaphore.
> It decrements the variable.
> Is it less than zero? Bingo! It owns it. Increment it twice or add two if you can be sure nothing will interfere with the addition.
> It is now one. YOU ARE THE OWNER
> If it doesn't become negative, you don't own it. Increment it back to previous state.
>
> If you become the owner, when done with the semaphore decrement it. (probably back to zero).
> When you don't own it, wait a while then try to acquire it again.
> A well behaved task returns the semaphore to zero in a reasonable amount of time.
> The acquiring task on failure to acquire waits for some time, then tries again.
> Optionally, after X failures it gives up and reports an error, probably by incrementing some other semaphore.
>
> You can use semaphore like this to have another task automatically launch a task elsewhere - a timer can monitor semaphore and launch a new task.
> The launched task decides whether the semaphore is available for further callers.
>
> Making all these triggers occur once per whatever cycle, in most cases will prevent race conditions. They become wait conditions, with optional timeout and the caller can even have knowledge of whether the task is active.
>
> I have found this to give reliable synchronization of asynchronous tasks.
>
> Neil
>